package top.sdwu_top.kbase.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.sdwu_top.kbase.common.annocation.CurrentUser;
import top.sdwu_top.kbase.common.model.Wrapper;
import top.sdwu_top.kbase.entity.KbaseAuthOrg;
import top.sdwu_top.kbase.entity.KbaseAuthUser;
import top.sdwu_top.kbase.service.IKbaseAuthOrgService;
import top.sdwu_top.kbase.vo.KbaseAuthOrgVo;

import java.util.List;

/**
 * <p>
 * 组织表 前端控制器
 * </p>
 *
 * @author zhipeng.duan@nvr-china.com
 * @since 2020-02-20
 */
@RestController
@RequestMapping("api/auth-org")
public class KbaseAuthOrgController {

    @Autowired
    IKbaseAuthOrgService orgService;


    @PostMapping("page")
    public Wrapper<Page<KbaseAuthOrg>> page(@CurrentUser KbaseAuthUser user,  @RequestBody KbaseAuthOrgVo authOrgVo) {
        return Wrapper.ok(orgService.page(user, authOrgVo));
    }

    @PostMapping
    public Wrapper<String> create(@CurrentUser KbaseAuthUser user,  @RequestBody KbaseAuthOrg authOrg) {
        if (orgService.save(user, authOrg)) {
            return Wrapper.ok("组织创建成功");
        }
        return Wrapper.error("组织创建失败");
    }

    @PutMapping
    public Wrapper<String> update(@CurrentUser KbaseAuthUser user, @RequestBody KbaseAuthOrg authOrg) {
        if (orgService.update(user, authOrg)) {
            return Wrapper.ok("组织修改成功");
        }
        return Wrapper.error("组织修改失败");
    }

    @DeleteMapping("{id}")
    public Wrapper<String> delete(@CurrentUser KbaseAuthUser user, @PathVariable("id") String id) {
        if (orgService.delete(user, id)) {
            return Wrapper.ok("组织删除成功");
        }
        return Wrapper.error("组织删除失败");
    }

    @GetMapping("{id}")
    public Wrapper<KbaseAuthOrg> get(@CurrentUser KbaseAuthUser user, @PathVariable("id") String id) {
        return Wrapper.ok(orgService.getChain(user, id));
    }

    @GetMapping("tree")
    public Wrapper<List<KbaseAuthOrg>> tree(@CurrentUser KbaseAuthUser user) {
        return Wrapper.ok(orgService.getTreeList(user));
    }


}

